package com.huage_03.lesson_03;

import com.huage_03.lesson_02.util.DruidUtils;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class SpringTemplateDemo {

    JdbcTemplate template = new JdbcTemplate(DruidUtils.ds);

    @Test
    public void testQuery(){
        String sql = "select * from user where id = ? or id = ? ";
        List<User> users = template.query(sql, new BeanPropertyRowMapper<User>(User.class), 1, 2);
        for (User user : users) {
            System.out.println(user);
        }
    }
    @Test
    public void testInsert(){
        String sql = "insert into user(id,username,password) value(null,'赵云','你好')";
        int count = template.update(sql);
        System.out.println(count);
    }
    @Test
    public void testUpdate(){
        String sql = "update user set password = '110' where username = ?";
        int count = template.update(sql,"zhangsan");
        System.out.println(count);
    }
    @Test
    public void testDelete(){
        String sql = "delete from  user  where username = ?";
        int count = template.update(sql,"赵云");
        System.out.println(count);
    }
}
